﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webapi.EFCore.IRepository;
using Webapi.EFCore.IService;
using Webapi.EFCore.Model;
using Webapi.EFCore.Model.Enity;
using Webapi.EFCore.Model.ViewModel;

namespace Webapi.EFCore.Web.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    public class UserController : BaseController
    {
        private readonly ILogger<UserController> _logger;
        private readonly IUserService _service;
        public UserController(ILogger<UserController> logger,
            IUserService service)
        {
            _logger = logger;
            _service = service;

        }
        /// <summary>
        /// 测试,异步接口，获取所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IEnumerable<User>> Get()
        {
            return await _service.GetList();
        }

        /// <summary>
        /// 测试日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult LogTest()
        {
            _logger.LogInformation("info 日志");
            _logger.LogDebug("debug 日志");
            _logger.LogError(new System.IO.IOException(), "io 错误");
            _logger.LogInformation("ip:{Ip},username{UserName},userid:{UserId}", "127.0.0.1", "admin", "1");
            return Ok();
        }

        /// <summary>
        /// 抛出异常
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Throw()
        {
            throw new System.IO.IOException();
        }


        /// <summary>
        /// 用户添加 (此用户添加，不合格，业务中没有这样的功能（原型上没有）)
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        //[HttpPost]
        //public async Task<MessageModel<CurdModel>> Add(User user)
        //{
        //    return await _service.Add(user);
        //}

        [HttpPost]
        public async Task<MessageModel<CurdModel>> Add(UserRoleModel user)
        {
            return await _service.AddRoleBat(user);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<UserViewModel> GetUserDetails(int id)
        {
            return await _service.GetUserDetails(id);
        }
    }
}
